package com.fifth.apartment.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.apartment.constant.MessageConstant;
import com.fifth.apartment.entity.BuildingInfo;
import com.fifth.apartment.result.Result;
import com.fifth.apartment.result.Results;
import com.fifth.apartment.service.BuildingInfoService;
import com.fifth.apartment.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 公寓信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-07-11
 */
@RestController
@RequestMapping("/admin/apartment")
@Api(tags = "楼栋接口")
@Slf4j
public class BuildingInfoController {
    @Autowired
    private BuildingInfoService service;

    @GetMapping("getBuildings")
    public Result findBuildingType(){

        try {
            BuildingInfo[] types = service.getBuildings();
            return new Result(true,"查询成功",types);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询管理员信息")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            BuildingInfo buildingInfo = service.getById(id);
            if (buildingInfo != null) {
                return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, buildingInfo);
            } else {
                return new Result(false, MessageConstant.QUERY_CHECKITEM_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, e.getMessage());
        }
    }

    @GetMapping("/findPage")
    @ApiOperation("条件分页查询楼栋信息")
    public Result findPage(PageQueryVO vo){
        try {
            Page<BuildingInfo> page = service.findPage(vo);
            if(page!=null){
                return new Result(true , MessageConstant.QUERY_CHECKITEM_SUCCESS , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(true , MessageConstant.QUERY_CHECKITEM_FAIL);
    }
    @PostMapping("addBuilding")
    @ApiOperation("新增楼栋信息")
    public Result addBuilding(@RequestBody BuildingInfo buildingInfo){
        try {
            boolean save = service.save(buildingInfo);
            if (save){
                return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.ADD_CHECKITEM_FAIL);
    }
    @PutMapping("update")
    @ApiOperation("更新楼栋信息")
    public Result update(@RequestBody BuildingInfo buildingInfo){
        try {
            boolean flag = service.updateById(buildingInfo);
            if(flag){
                return new Result(true , MessageConstant.EDIT_CHECKITEM_SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.EDIT_CHECKITEM_FAIL);
    }

    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    @ApiOperation("更新楼栋上架状态")
    public Results updateReleaseStatusById(@RequestParam Long id, @RequestParam Boolean status) {

        LambdaUpdateWrapper<BuildingInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BuildingInfo::getId,id);
        updateWrapper.set(BuildingInfo::getIsRelease,status);
        service.update(updateWrapper);
        return Results.ok();
    }
}
